马在某个点最多可能有8种走法,用递归和回溯实现。
注:代码中,查找下一个可走坐标是从右下第一个开始的,也就是图中的4。可以通过修改a,b...h的值来改变顺序。
/**
* 马踏棋盘算法
* 递归和回溯
*
*/
public class HorseStep {
public static int X = 8;
public static int Y = 8;
public static int returnCount = 0;
/**
* 棋盘
*/
public static int chess[][] = new int[X][Y];
/**
* 找到基于(x,y)位置的下一个可走位置
* @param x
* @param y
* @param count
* @return
*/
public static int nextxy(XY xy,int count){
final int a=0,
b=1,
c=2,
d=3,
e=4,
f=5,
g=6,
h=7;
int x = xy.getX();
int y = xy.getY();
int returnInt = 0;
switch (count)